package entity

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/gorm"
)

type MemberIds []uint

func (m MemberIds) Value() (driver.Value, error) {
	return json.Marshal(m)
}

func (m *MemberIds) Scan(value interface{}) error {
	bytes, ok := value.([]byte)
	if !ok {
		return fmt.Errorf("类型断言失败")
	}
	return json.Unmarshal(bytes, m)
}

type Group struct {
	gorm.Model
	GroupName string    `json:"group_name"`
	OwnerUser uint      `json:"owner_user"`
	Members   MemberIds `json:"member_id" gorm:"type:json"`
}

func (Group) TableName() string {
	return "group"
}

func (g Group) String() string {
	return fmt.Sprintf(
		"Group{ID: %d, GroupName: %s, OwnerUser: %v, Members: %v}",
		g.ID,
		g.GroupName,
		g.OwnerUser,
		g.Members,
	)
}
